<?php
/*
 * @Descripttion: 
 * @Author: 荷逸
 * @email: 563428234@qq.com
 * @Date: 2021-01-09 16:58:32
 */

declare(strict_types=1);


namespace app\admin\service;

use app\admin\model\FilesDirectory;

class DirectoryService
{
    private  $File = [];
    public function getSearch($dir, $param)
    {
        $directory = new FilesDirectory($dir);
        $data = $directory->list();
        if (isset($data) && !empty($data)) {
            foreach ($data as $item) {
                if (isset($param['search']) && strstr($item['base_name'], $param['search']) != false) {
                    $this->File[] = $item;
                }
                if ($item['type'] == 'directory') {
                    $this->getSearch($dir . '/' . $item['base_name'], $param);
                }
            }
        }
        return $this->File;
    }

    public function getList($dir, $param)
    {

        $directory = new FilesDirectory($dir);
        $data = $directory->list();
        if (isset($data) && !empty($data)) {
            foreach ($data as $vo) {
                $this->File[] = $vo;
            }
        }

        return $this->File;
    }
    public function decompose()
    {

        foreach ($this->File as $vo) {
            if ($vo['type'] == 'directory') {
                $dirFile[] = $vo;
            } else {
                $file[] = $vo;
            }
        }
        if (isset($dirFile)) {
            array_multisort(array_column($dirFile, 'base_name'), SORT_ASC, $dirFile);
        } else {
            $dirFile = [];
        }
        if (isset($file)) {
            array_multisort(array_column($file, 'base_name'), SORT_ASC, $file);
        } else {
            $file = [];
        }
        $info = ['dirFile' => $dirFile, 'file' => $file];
        return $info;
    }
}
